//
//  TTCoreUI.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTCoreUI.h"
#import "TTCoreCommon.h"


#pragma mark - Autoresizing mask

const NSUInteger TTViewAutoresizingKeepSize = 
                    UIViewAutoresizingFlexibleLeftMargin | 
                    UIViewAutoresizingFlexibleTopMargin | 
                    UIViewAutoresizingFlexibleRightMargin | 
                    UIViewAutoresizingFlexibleBottomMargin | 
                    !UIViewAutoresizingFlexibleWidth | 
                    !UIViewAutoresizingFlexibleHeight;

const NSUInteger TTViewAutoresizingKeepMargin = 
                    !UIViewAutoresizingFlexibleLeftMargin | 
                    !UIViewAutoresizingFlexibleTopMargin | 
                    !UIViewAutoresizingFlexibleRightMargin | 
                    !UIViewAutoresizingFlexibleBottomMargin | 
                    UIViewAutoresizingFlexibleWidth | 
                    UIViewAutoresizingFlexibleHeight;



#pragma mark - Dimensions constants

const CGFloat TTStatusBarHeight = 20.0;
const CGFloat TTTableViewCellHeight = 44.0;

const CGFloat TTToolbarHeightInPortrait = 44.0;
const CGFloat TTToolbarHeightInLandscape = 32.0;

const CGFloat TTNavigationBarHeightInPortrait = 44.0;
const CGFloat TTNavigationBarHeightInLandscape = 32.0;

const CGFloat TTKeyboardHeightInPortrait = 216.0;
const CGFloat TTKeyboardHeightInLandscape = 160.0;
const CGFloat TTPadKeyboardHeightInPortrait = 264.0;
const CGFloat TTPadKeyboardHeightInLandscape = 352.0;



#pragma mark - Orientation

UIDeviceOrientation TTDeviceOrientation()
{
    return [UIDevice currentDevice].orientation;
}

UIInterfaceOrientation TTInterfaceOrientation()
{
    return [UIApplication sharedApplication].statusBarOrientation;
}



BOOL TTIsDevicePortrait()
{
    UIDeviceOrientation orient = TTDeviceOrientation();
    return ((orient == UIDeviceOrientationPortrait) || 
            (orient == UIDeviceOrientationPortraitUpsideDown)
            );
}

BOOL TTIsDeviceLandscape()
{
    UIDeviceOrientation orient = TTDeviceOrientation();
    return ((orient == UIDeviceOrientationLandscapeLeft) || 
            (orient == UIDeviceOrientationLandscapeRight)
            );
}

BOOL TTIsInterfacePortrait()
{
    UIInterfaceOrientation orient = TTInterfaceOrientation();
    return ((orient == UIInterfaceOrientationPortrait) || 
            (orient == UIInterfaceOrientationPortraitUpsideDown)
            );
}

BOOL TTIsInterfaceLandscape()
{
    UIInterfaceOrientation orient = TTInterfaceOrientation();
    return ((orient == UIInterfaceOrientationLandscapeLeft) || 
            (orient == UIInterfaceOrientationLandscapeRight)
            );
}



BOOL TTIsSupportedOrientation(UIInterfaceOrientation orientation)
{
    if ( TTIsPad() ) {
        return YES;
    }
    
    if ((orientation == UIInterfaceOrientationPortrait) || 
        (orientation == UIInterfaceOrientationLandscapeLeft) || 
        (orientation == UIInterfaceOrientationLandscapeRight))
    {
        return YES;
    }
    
    return NO;
}

CGAffineTransform TTRotateTransformForOrientation(UIInterfaceOrientation orientation)
{
    if ( orientation == UIInterfaceOrientationLandscapeLeft ) {
        return CGAffineTransformMakeRotation(M_PI * 1.5);
    } else if ( orientation == UIInterfaceOrientationLandscapeRight ) {
        return CGAffineTransformMakeRotation(M_PI / 2.0);
    } else if ( orientation == UIInterfaceOrientationPortraitUpsideDown ) {
        return CGAffineTransformMakeRotation(0.0 - M_PI);
    }
    return CGAffineTransformIdentity;
}

CGAffineTransform TTSupportedRotateTransformForOrientation(UIInterfaceOrientation orientation)
{
    if ( orientation == UIInterfaceOrientationLandscapeLeft ) {
        return CGAffineTransformMakeRotation(M_PI * 1.5);
    } else if ( orientation == UIInterfaceOrientationLandscapeRight ) {
        return CGAffineTransformMakeRotation(M_PI / 2.0);
    }
    return CGAffineTransformIdentity;
}



CGFloat TTToolbarHeightForOrientation(UIInterfaceOrientation orientation)
{
    if (UIInterfaceOrientationIsPortrait(orientation) || 
        TTIsPad())
    {
        return TTToolbarHeightInPortrait;
    }
    return TTToolbarHeightInLandscape;
}

CGFloat TTNavigationBarHeightForOrientation(UIInterfaceOrientation orientation)
{
    if (UIInterfaceOrientationIsPortrait(orientation) || 
        TTIsPad())
    {
        return TTNavigationBarHeightInPortrait;
    }
    return TTNavigationBarHeightInLandscape;
}

CGFloat TTKeyboardHeightForOrientation(UIInterfaceOrientation orientation)
{
    if ( TTIsPad() ) {
        if ( TTIsInterfacePortrait() ) {
            return TTPadKeyboardHeightInPortrait;
        } else {
            return TTPadKeyboardHeightInLandscape;
        }
    } else {
        if ( TTIsInterfacePortrait() ) {
            return TTKeyboardHeightInPortrait;
        } else {
            return TTKeyboardHeightInLandscape;
        }
    }
    return 0.0;
}



#pragma mark - Keyboard

BOOL TTIsKeyboardVisible()
{
    // Operates on the assumption that the keyboard is visible if and only if there is a first
    // responder; i.e. a control responding to key events.
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *firstResponder = TTFindFirstResponderInView(window);
    return ( firstResponder != nil );
}

UIView *TTFindFirstResponderInView(UIView *topView)
{
    if ( [topView isFirstResponder] ) {
        return topView;
    }
    
    for ( UIView *subview in topView.subviews ) {
        if ( [subview isFirstResponder] ) {
            return subview;
        }
        
        UIView *firstResponder = TTFindFirstResponderInView(subview);
        if ( firstResponder ) {
            return firstResponder;
        }
    }
    return nil;
}
